﻿using System.Collections;
using WatiN.Core;
using System;

namespace QAHelper.Common
{
    public static class WatiNInputHelper
    {
        private static Browser window = null;
        #region Set Page Input method

        #region Set Page Input with two parameters
        public static void SetPageInput(Browser window, DictionaryEntry[] input_data)
        {
            foreach (DictionaryEntry dataRow in input_data)
            {
                if (dataRow.Key is System.DBNull && dataRow.Value is System.DBNull)
                    continue;
                string control_name = dataRow.Key.ToString();
                string control_value = dataRow.Value.ToString();

                if (window.TextField(Find.ById(control_name)).Exists)
                {
                    window.TextField(Find.ById(control_name)).TypeText(control_value);
                }
                else if (window.SelectList(Find.ById(control_name)).Exists)
                {
                    SelectList select = window.SelectList(Find.ById(control_name));
                    if (select.Multiple)
                    {
                        string[] values = control_value.Split(',');
                        foreach (string str in values)
                        {
                            select.Select(str);
                        }
                    }
                    else
                    {
                        window.SelectList(Find.ById(control_name)).Select(control_value);
                    }
                }
                else if (window.RadioButton(Find.ById(control_name)).Exists)
                {
                    if (control_value.Equals("true", System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        window.RadioButton(Find.ById(control_name)).Checked = true;
                    }
                    else
                    {
                        window.RadioButton(Find.ById(control_name)).Checked = false;
                    }
                }
                else if (window.CheckBox(control_name).Exists)
                {
                    if (control_value.Equals("true", System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        window.CheckBox(Find.ById(control_name)).Checked = true;
                    }
                    else
                    {
                        window.CheckBox(Find.ById(control_name)).Checked = false;
                    }
                }
            }
        }
        #endregion

        #region Method with 3rd parameter which is a out parameter.
        public static void SetPageInput(Browser window, DictionaryEntry[] input_data, out int no_of_input_fields)
        {
            no_of_input_fields = 0;

            foreach (DictionaryEntry dataRow in input_data)
            {
                if (dataRow.Key is System.DBNull && dataRow.Value is System.DBNull)
                    continue;
                string control_name = dataRow.Key.ToString();
                string control_value = dataRow.Value.ToString();

                if (window.TextField(Find.ById(control_name)).Exists)
                {
                    window.TextField(Find.ById(control_name)).TypeText(control_value);
                    no_of_input_fields++;
                }
                else if (window.SelectList(Find.ById(control_name)).Exists)
                {
                    SelectList select = window.SelectList(Find.ById(control_name));
                    if (select.Multiple)
                    {
                        string[] values = control_value.Split(',');
                        foreach (string str in values)
                        {
                            select.Select(str);
                        }
                    }
                    else
                    {
                        window.SelectList(Find.ById(control_name)).Select(control_value);
                    }
                    no_of_input_fields++;
                }
                else if (window.RadioButton(Find.ById(control_name)).Exists)
                {
                    if (control_value.Equals("true", System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        window.RadioButton(Find.ById(control_name)).Checked = true;
                    }
                    else
                    {
                        window.RadioButton(Find.ById(control_name)).Checked = false;
                    }
                    no_of_input_fields++;
                }
                else if (window.CheckBox(control_name).Exists)
                {
                    if (control_value.Equals("true", System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        window.CheckBox(Find.ById(control_name)).Checked = true;
                    }
                    else
                    {
                        window.CheckBox(Find.ById(control_name)).Checked = false;
                    }
                    no_of_input_fields++;
                }
            }
        }
        #endregion

        #region Method with 3rd parameter which is a out parameter.
        public static void SetPageInput(Browser window, ArrayList input_data, out int no_of_input_fields)
        {
            WatiNInputHelper.window = window;

            no_of_input_fields = 0;
            bool checkedVal=false;
            foreach (DictionaryEntry dataRow in input_data)
            {
                if (dataRow.Key is System.DBNull && dataRow.Value is System.DBNull)
                    continue;
                string control_name = dataRow.Key.ToString().Trim();
                string control_value = dataRow.Value.ToString().Trim();

                if (window.TextField(Find.ById(control_name)).Exists)
                {
                    window.TextField(Find.ById(control_name)).TypeText(control_value);
                    no_of_input_fields++;
                }
                else if (window.TextField(Find.ByName(control_name)).Exists && !window.TextField(Find.ByName(control_name)).GetAttributeValue("type").Equals("hidden"))
                {
                    window.TextField(Find.ByName(control_name)).TypeText(control_value);
                    no_of_input_fields++;
                }
                else if (window.FileUpload(Find.ById(control_name)).Exists)
                {
                    window.FileUpload(Find.ById(control_name)).Set(control_value);
                    no_of_input_fields++;
                }
                else if (window.FileUpload(Find.ByName(control_name)).Exists)
                {
                    window.FileUpload(Find.ByName(control_name)).Set(control_value);
                    no_of_input_fields++;
                }
                else if (window.SelectList(Find.ById(control_name)).Exists)
                {
                    SelectList select = window.SelectList(Find.ById(control_name));
                    if (select.Multiple)
                    {
                        SelectMultiple(select, "select",control_name, control_value, true);
                    }
                    else
                    {
                        window.SelectList(Find.ById(control_name)).SelectByValue(control_value);
                    }
                    no_of_input_fields++;
                }
                else if (window.SelectList(Find.ByName(control_name)).Exists)
                {
                    SelectList select = window.SelectList(Find.ByName(control_name));
                    if (select.Multiple)
                    {
                        SelectMultiple(select, "select",control_name, control_value, false);
                    }
                    else
                    {
                        window.SelectList(Find.ByName(control_name)).SelectByValue(control_value);
                    }
                    no_of_input_fields++;
                }
                else if (window.RadioButton(Find.ById(control_name)).Exists)
                {
                    RadioButton rdo = window.RadioButton(Find.ById(control_name));
                    string rdo_name = rdo.GetAttributeValue("name");
                    if (!string.IsNullOrEmpty(rdo_name))
                    {
                        rdo = window.RadioButton(Find.ById(control_name).And(Find.ByName(rdo_name).And(Find.ByValue(control_value))));
                    }
                    rdo.Checked = true;

                    no_of_input_fields++;
                }
                else if (window.CheckBox(Find.ById(control_name)).Exists)
                {
                    CheckBox chk = null;
                    if (control_value.Contains(","))
                    {
                        SelectMultiple(chk, "check", control_name,control_value, true);
                        no_of_input_fields++;
                    }             
                    else if (bool.TryParse(control_value,out checkedVal))
                    {
                        window.CheckBox(Find.ById(control_name)).Checked = Convert.ToBoolean(control_value.ToLower());
                        no_of_input_fields++;
                    }
                    else
                    {
                        chk = window.CheckBox(Find.ById(control_name) & Find.ByValue(control_value.Trim()));
                        chk.Checked = true;
                        no_of_input_fields++;
                    }
                }
                else if (window.CheckBox(Find.ByName(control_name)).Exists)
                {
                    CheckBox chk = null;
                    if (control_value.Contains(","))
                    {
                        SelectMultiple(chk, "check", control_name, control_value, false);
                        no_of_input_fields++;
                    }
                    else if (bool.TryParse(control_value, out checkedVal))
                    {
                        window.CheckBox(Find.ByName(control_name)).Checked = Convert.ToBoolean(control_value.ToLower());
                        no_of_input_fields++;
                    }
                    else
                    {
                        chk = window.CheckBox(Find.ByName(control_name) & Find.ByValue(control_value.Trim()));
                        chk.Checked = true;
                        no_of_input_fields++;
                    }
                }
            }
        }
        #endregion

        #region Method with 4th parameter which to decide whethe fireevent need to trigger or not
        public static void SetPageInput(Browser window, ArrayList input_data, out int no_of_input_fields, string controlToFireEvent)
        {
            no_of_input_fields = 0;

            foreach (DictionaryEntry dataRow in input_data)
            {
                if (dataRow.Key is System.DBNull && dataRow.Value is System.DBNull)
                    continue;
                string control_name = dataRow.Key.ToString().Trim();
                string control_value = dataRow.Value.ToString().Trim();
                
                if (window.TextField(Find.ById(control_name)).Exists)
                {
                    window.TextField(Find.ById(control_name)).TypeText(control_value);
                    no_of_input_fields++;
                }
                else if (window.SelectList(Find.ById(control_name)).Exists)
                {
                    SelectList select = window.SelectList(Find.ById(control_name));
                    if (select.Multiple)
                    {
                        string[] values = control_value.Split(',');
                        foreach (string str in values)
                        {
                            select.Select(str);
                        }
                    }
                    else
                    {
                        window.SelectList(Find.ById(control_name)).Select(control_value);
                    }
                    no_of_input_fields++;
                }
                else if (window.RadioButton(Find.ById(control_name)).Exists)
                {
                    window.RadioButton(Find.ById(control_name)).Checked = true;
                    no_of_input_fields++;
                }
                else if (window.CheckBox(control_name).Exists)
                {
                    if (window.CheckBox(control_name).Enabled)
                    {
                        if (!string.IsNullOrEmpty(controlToFireEvent))
                        {
                            if (control_name.Contains(controlToFireEvent))
                            {
                                window.CheckBox(Find.ById(control_name)).FireEvent("onclick");
                                no_of_input_fields++;
                            }
                            else if (control_value.Equals("true",System.StringComparison.InvariantCultureIgnoreCase))
                            {
                                window.CheckBox(Find.ById(control_name)).Checked = true;
                                no_of_input_fields++;
                            }
                        }
                    }
                    //window.CheckBox(Find.ById(control_name)).Checked = true;
                    //no_of_input_fields++;
                }
            }
        }
        #endregion

        #endregion

        #region Open Pop-up Test
        /// <summary>
        /// Opens the respective pop-up
        /// </summary>
        /// <param name="window">Browser object</param>
        /// <param name="controlName">Control name for which the pop-up will appear</param>
        /// <param name="buttonName">Button label text clicking on which the pop-up will appear</param>
        /// <param name="divId">Div ID of the pop-up</param>
        /// <param name="validationText">Text to validate when the pop-up appears</param>
        /// <param name="backgroundDiv">Background div id</param>
        /// <returns>Returns status of the action in string format</returns>
        private static string OpenPopUpTest(Browser window, string controlName, string buttonName, string divId, string validationText, string backgroundDiv)
        {
            Button button = window.Button(Find.ByValue(buttonName));
            string result = null;

            button.Click();
            if (validationText != null)
            {
                if (!window.Div(Find.ById(divId)).Enabled && !window.ContainsText(validationText)) result += Environment.NewLine + controlName + " pop-up does not appear.";
            }
            else
            {
                if (!window.Div(Find.ById(divId)).Enabled) result += Environment.NewLine + controlName + " pop-up does not appear.";
            }

            if (!window.Div(Find.ById(divId)).Enabled) result += Environment.NewLine + "Backgound is not disabled.";

            if (result == null) result += Environment.NewLine + controlName + " pop-up appears successfully and backgound is disabled properly.";

            return result;
        }
        #endregion

        private static void SelectMultiple(Element control,string controlTypeName, string paramControlName, string paramControlValue, bool isById)
        {
            //string name = control.GetAttributeValue("name");
            //string id = control.GetAttributeValue("id");
            string[] values = paramControlValue.Split(',');
            foreach (string str in values)
            {
                switch (controlTypeName.ToLower())
                {
                    case("select"):
                        SelectList select = (SelectList)control;                        
                        select.SelectByValue(paramControlValue);
                        break;
                    case("check"):
                        CheckBox chk = (CheckBox)control;
                        if (isById)
                        {
                            chk = window.CheckBox(Find.ById(paramControlName) & Find.ByValue(str.Trim()));
                        }
                        else
                            chk = window.CheckBox(Find.ByName(paramControlName) & Find.ByValue(str.Trim()));
                        chk.Checked = true;

                        break;
                    case("radio"):
                        RadioButton rdo = (RadioButton)control;
                        rdo = window.RadioButton(Find.ById(paramControlName) & Find.ByValue(str.Trim()));
                        rdo.Checked = true;
                        break;
                }
            }
        }
    }
}
